<template>
  <el-form ref="deptForm" label-width="120px" :model="form" :rules="rules">
    <el-form-item label="部门名称" prop="name">
      <el-input
        v-model="form.name"
        style="width: 80%"
        placeholder="1-50个字符"
      />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input
        v-model="form.code"
        style="width: 80%"
        placeholder="1-50个字符"
      />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width: 80%" placeholder="请选择">
        <el-option
          v-for="item in list"
          :key="item.id"
          :value="item.username"
          :label="item.username"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input
        v-model="form.introduce"
        style="width: 80%"
        placeholder="1-300个字符"
        type="textarea"
        :rows="3"
      />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="addbm">确定</el-button>
      <el-button size="small" @click="hCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { addbumen, emitbumen, getbumen, doemit } from '@/api/departments'
export default {
  props: {
    pid: {
      required: true,
      type: String
    },
    isEmit: {
      required: true,
      type: Boolean
    },
    xiaoyanlist: {
      required: true,
      type: Array
    }
  },
  data() {
    const validator1 = (rule, value, callback) => {
      const id = this.pid
      console.log(id)
      const arr = this.xiaoyanlist.filter(item => item.pid === id).map(item => item.name)

      if (!this.isEmit) {
        if (arr.includes(value)) { callback(new Error(value + '占用')) }
      } else {
        const pid = this.xiaoyanlist.filter(item => item.id === id).map(item => item.pid)[0]
        const namelist = this.xiaoyanlist.filter(item => item.pid === pid).map(item => item.name).filter(item => item !== this.xiaoyanlist.filter(item => item.id === id).map(item => item.name)[0])
        if (namelist.includes(value)) {
          callback(new Error(value + '占用'))
        }
      }
      callback()
    }
    const validator2 = (rule, value, callback) => {
      const id = this.pid
      console.log(id)
      const arr = this.xiaoyanlist.filter(item => item.pid === id).map(item => item.code)

      if (!this.isEmit) {
        if (arr.includes(value)) { callback(new Error(value + '占用')) }
      } else {
        const pid = this.xiaoyanlist.filter(item => item.id === id).map(item => item.pid)[0]
        const namelist = this.xiaoyanlist.filter(item => item.pid === pid).map(item => item.code).filter(item => item !== this.xiaoyanlist.filter(item => item.id === id).map(item => item.code)[0])
        console.log(namelist, value)
        console.log(namelist.includes(value))
        if (namelist.includes(value)) {
          callback(new Error(value + '占用'))
        }
      }
      callback()
    }
    return {
      list: [],
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [{ required: true, message: '请输入内容', trigger: 'blur' },
          { min: 1, max: 50, message: '请输入1-50字符' },
          { validator: validator1, trigger: 'blur' }],
        code: [{ required: true, message: '请输入内容', trigger: 'blur' },
          { min: 1, max: 50, message: '请输入1-50字符' },
          { validator: validator2, trigger: 'blur' }],

        manager: [{ required: true, message: '请输入内容', trigger: 'blur' }],
        introduce: [{ required: true, message: '请输入内容', trigger: 'blur' },
          { min: 1, max: 50, message: '请输入1-50字符' }]
      }
    }
  },
  // watch: {
  //   pid(newVal, old) {
  //     console.log(newVal)
  //   }
  // },

  created() {
    this.getlist()
    // if (this.isEmit) {
    //   this.emitbumens()
    // }
  },
  methods: {
    async getlist() {
      const { data: res } = await getbumen()
      this.list = res
    },
    async addbm() {
      // const obj = { ...this.form, pid: this.pid }
      if (this.isEmit) {
        this.daEmit()
      } else {
        this.addbumens()
      }
    },
    async  daEmit() {
      const data = await doemit(this.form)
      console.log(data)
    },
    async addbumens() {
      this.form.pid = this.pid
      await addbumen(this.form)
      this.$emit('success')
    },
    async emitbumens() {
      const { data: res } = await emitbumen(this.pid)
      // console.log(res)
      this.form = res
    },
    hCancel() {
      this.$emit('quxiao')
    }
  }
}
</script>

<style></style>
